﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;

//CID 0921 //2011.05.22 //Begin
namespace InfoJetSoft.Service.Util
{
    class XPathQuery
    {
        public static bool QueryFieldRuleConditionValue(Form form, Rule rule, XmlNode contextNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleConditionValue(form, rule, contextNode);
            }
            else
            {
                XmlDocument ruleResultDoc = QueryXsltRunner.runXslt(form, rule.QueryXslt);
                if (QueryXsltResultUtil.isFieldElementTextIsTrue(ruleResultDoc, contextId))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool QueryButtonRuleConditionValue(Form form, Rule rule, XmlNode contextNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleConditionValue(form, rule, contextNode);
            }
            else
            {
                XmlDocument ruleResultDoc = QueryXsltRunner.runXslt(form, rule.QueryXslt, contextId);
                if (QueryXsltResultUtil.existTrueResult(ruleResultDoc))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool QueryOnLoadRuleConditionValue(Form form, Rule rule)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleConditionValue(form, rule, form.XmlDoc.DocumentElement);
            }
            else
            {
                XmlDocument ruleResultDoc = QueryXsltRunner.runXslt( form, rule.QueryXslt );
                if (QueryXsltResultUtil.existTrueResult(ruleResultDoc))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool QuerySectionRuleConditionValue(Form form, Rule rule, XmlNode insertedNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleConditionValue(form, rule, insertedNode);
            }
            else
            {
                XmlDocument ruleResultDoc = QueryXsltRunner.runXslt(form, rule.QueryXslt);
                if (QueryXsltResultUtil.isFieldElementTextIsTrue(ruleResultDoc, contextId))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        private static bool QueryRuleConditionValue(Form form, Rule rule, XmlNode contextNode)
        {
            bool conditionValue = XPathEvaluator.evaluateBooleanValue(form, contextNode, rule.Condition);
            return conditionValue;
        }

        public static string QueryFieldRuleActionValue(Form form, RuleAction ruleAction, XmlNode contextNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleActionValue(form, ruleAction, contextNode);   
            }
            else
            {
                XmlDocument ruleActionResultDoc = QueryXsltRunner.runXslt(form, ruleAction.QueryXslt);
                XmlElement exprElement = QueryXsltResultUtil.findFieldElementByJetId(ruleActionResultDoc, contextId);
                if (exprElement != null)
                {
                    string exprValue = exprElement.InnerText;
                    return exprValue;
                }
            }
            return null;
        }

        public static string QueryButtonRuleActionValue(Form form, RuleAction ruleAction, XmlNode contextNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleActionValue(form, ruleAction, contextNode);
            }
            else
            {
                XmlDocument ruleActionResultDoc = QueryXsltRunner.runXslt(form, ruleAction.QueryXslt, contextId);
                string exprValue = QueryXsltResultUtil.findFieldValue(ruleActionResultDoc);
                return exprValue;
            }
        }

        public static string QueryOnLoadRuleActionValue(Form form, RuleAction ruleAction)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleActionValue(form, ruleAction, form.XmlDoc.DocumentElement);
            }
            else
            {
                XmlDocument ruleActionResultDoc = QueryXsltRunner.runXslt(form, ruleAction.QueryXslt);
                string exprValue = QueryXsltResultUtil.findFieldValue(ruleActionResultDoc);
                return exprValue;
            }
        }

        public static string QuerySectionRuleActionValue(Form form, RuleAction ruleAction, XmlNode insertedNode, string contextId)
        {
            if (form.Manifest.EnableBindingTest)
            {
                return QueryRuleActionValue(form, ruleAction, insertedNode);
            }
            else
            {
                XmlDocument ruleActionResultDoc = QueryXsltRunner.runXslt(form, ruleAction.QueryXslt);
                XmlElement exprElement = QueryXsltResultUtil.findFieldElementByJetId(ruleActionResultDoc, contextId);
                if (exprElement != null)
                {
                    string exprValue = exprElement.InnerText;
                    return exprValue;
                }
            }
            return null;
        }

        private static string QueryRuleActionValue(Form form, RuleAction ruleAction, XmlNode contextNode)
        {
            if (ruleAction.RuleActionType == RuleAction.iDialogBoxExpression)
            {
                string actionValue = XPathEvaluator.evaluateStringValue(form, contextNode, ruleAction.DialogBoxExpression);
                return actionValue;
            }
            else if (ruleAction.RuleActionType == RuleAction.iAssignment)
            {
                string actionValue = XPathEvaluator.evaluateStringValue(form, contextNode, ruleAction.AssignmentExpression);
                return actionValue;
            }
            else if (ruleAction.RuleActionType == RuleAction.iChangeAdapterProperty)
            {
                string actionValue = XPathEvaluator.evaluateStringValue(form, contextNode, ruleAction.Expression);
                return actionValue;
            }
            else
            {
                return null;
            }
        }

        public static string QueryEmailToValue(Form form, DataEmailAdapter emailAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string mailTo = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, emailAdapter.ToValue);
                return mailTo;
            }
            else
            {
                XmlDocument toDoc = QueryXsltRunner.runXslt(form, emailAdapter.ToQueryXslt);
                string mailTo = QueryXsltResultUtil.findFieldValue(toDoc);
                return mailTo;
            }
        }

        public static string QueryEmailCcValue(Form form, DataEmailAdapter emailAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string mailCc = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, emailAdapter.CcValue);
                return mailCc;
            }
            else
            {
                XmlDocument ccDoc = QueryXsltRunner.runXslt(form, emailAdapter.CcQueryXslt);
                string mailCc = QueryXsltResultUtil.findFieldValue(ccDoc);
                return mailCc;
            }
        }

        public static string QueryEmailBccValue(Form form, DataEmailAdapter emailAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string mailBcc = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, emailAdapter.BccValue);
                return mailBcc;
            }
            else
            {
                XmlDocument bccDoc = QueryXsltRunner.runXslt(form, emailAdapter.BccQueryXslt);
                string mailBcc = QueryXsltResultUtil.findFieldValue(bccDoc);
                return mailBcc;
            }
        }

        public static string QueryEmailSubjectValue(Form form, DataEmailAdapter emailAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string mailSubject = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, emailAdapter.SubjectValue);
                return mailSubject;
            }
            else
            {
                XmlDocument subjectDoc = QueryXsltRunner.runXslt(form, emailAdapter.SubjectQueryXslt);
                string mailSubject = QueryXsltResultUtil.findFieldValue(subjectDoc);
                return mailSubject;
            }
        }

        public static string QueryEmailAttachmentFileNameValue(Form form, DataEmailAdapter emailAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string mailAttachName = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, emailAdapter.AttachmentFileNameValue);
                return mailAttachName;
            }
            else
            {
                XmlDocument attachNameDoc = QueryXsltRunner.runXslt(form, emailAdapter.AttachmentFileNameQueryXslt);
                string mailAttachName = QueryXsltResultUtil.findFieldValue(attachNameDoc);
                return mailAttachName;
            }
        }

        public static string QueryDavFileNameValue(Form form, DataDavAdapter davAdapter)
        {
            if (form.Manifest.EnableBindingTest)
            {
                string fileName = XPathEvaluator.evaluateStringValue(form, form.XmlDoc.DocumentElement, davAdapter.FileNameValue);
                return fileName;
            }
            else
            {
                XmlDocument fileNameDoc = QueryXsltRunner.runXslt(form, davAdapter.QueryXslt);
                string fileName = QueryXsltResultUtil.findFieldValue(fileNameDoc);
                return fileName;
            }
        }

        public static List<XmlNode> QueryErrorCondition(Form form, ErrorCondition errorCondition)
        {
            List<XmlNode> errorNodes = new List<XmlNode>();
            if (form.Manifest.EnableBindingTest)
            {
                XmlNodeList matchNodes = DOMUtil.selectNodes(form.XmlDoc.DocumentElement, errorCondition.Match, 0, form.XmlNsResolver);
                foreach (XmlNode matchNode in matchNodes)
                {
                    XmlNodeList contextNodes = DOMUtil.selectNodes(matchNode, errorCondition.ExpressionContext, 0, form.XmlNsResolver);
                    foreach (XmlNode contextNode in contextNodes)
                    {
                        bool isError = XPathEvaluator.evaluateBooleanValue(form, contextNode, errorCondition.Expression);
                        if (isError)
                        {
                            errorNodes.Add(contextNode);
                        }
                    }
                }
            }
            else
            {
                XmlDocument resultDoc = QueryXsltRunner.runXslt(form, errorCondition.QueryXslt);
                XmlNodeList fieldElementList = resultDoc.SelectNodes("/result/field");
                if (fieldElementList.Count > 0)
                {
                    foreach (XmlElement fieldElement in fieldElementList)
                    {
                        if (fieldElement.InnerText.Trim().ToLower().Equals("true"))
                        {
                            string id = fieldElement.GetAttribute(JetConstant.JetId);
                            string attr = fieldElement.GetAttribute(JetConstant.AttributeName);
                            string jetId = null;
                            if (attr != null && attr.Length > 0)
                            {
                                jetId = id + "@" + attr;
                            }
                            else
                            {
                                jetId = id;
                            }
                            XmlNode xmlNode = DOMUtil.findNodeByJetId(form, jetId);
                            errorNodes.Add(xmlNode);
                        }
                    }
                }
            }
            return errorNodes;
        }
    }
}
//CID 0921 //2011.05.22 //End